Regex - Match attribute in an HTML code
Regex - Match attribute in an HTML code
540
29-Aug-2023
Updated on 04-Sep-2023
Aryan Kumar
04-Sep-2023Sure, here is a regular expression to match an attribute in an HTML code:
The
<metacharacter matches the beginning of an HTML tag. The\w+metacharacter matches one or more word characters. The\w+metacharacter matches one or more word characters. The=\"metacharacter matches an equal sign. The([^\"]+)part of the pattern matches any character except a double quote, and the\">metacharacter matches the end of an HTML tag.So, this regex pattern will match any HTML tag that has an attribute with a value.
For example, the following HTML code would be matched by this regex pattern:
HTML
The
typeandnameattributes would be matched by the pattern.The following HTML code would not be matched by this regex pattern:
HTML
The
h1tag does not have any attributes.